package com.dealinga.client;

import java.util.Collection;
import java.util.Date;

import com.dealinga.shared.Server;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventListener;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

public class DealingaOpen implements EntryPoint {

	private final ServiceAsync service = GWT.create(Service.class);

	public void onModuleLoad() {

/*		FlowPanel contentFlow = new FlowPanel();
		RootPanel.get("content").add(contentFlow);
		contentFlow.add(new DealSmall());
*/
		
		final Label infoLabel = Label.wrap(RootPanel.get("info").getElement());
		infoLabel.setText("Started" + new Date());
		
		final TextBox email = TextBox.wrap(RootPanel.get("email").getElement());
		email.setText("@");

		// test ui for mouse 
		DOM.sinkEvents(RootPanel.get().getElement(), Event.ONCLICK | Event.ONMOUSEOUT | Event.ONMOUSEOVER);
		DOM.setEventListener(RootPanel.get().getElement(), new EventListener() {
			@Override
			public void onBrowserEvent(Event event) {
				infoLabel.setText(new Date() + "X:" +  event.getScreenX() + "Y:" + event.getScreenX() + ",Type:" + event.getType().toString() + " - " + event.getButton());
/*			    if (event.equals(Event.ONMOUSEOVER)) {
			      Window.alert("ONMOUSEOVER");
			    } else if (event.equals(Event.ONMOUSEOUT)) {
			    } else if (event.equals(Event.ONCLICK)) {
			      Window.alert("ONCLICK");
			    }
*/			}
		});

		// assign user to cookies
		Collection<String> cookieNames = Cookies.getCookieNames();
		for (String name : cookieNames) {
			String value = Cookies.getCookie(name);
			if (!value.isEmpty() && !value.equals("null") && name.equals("token")) {
				//String token = GWT.getUniqueThreadId();
				Session.iu.token = value;
			} 
		}
		

		// registate session on server
		service.registrate(Session.iu , new AsyncCallback<Server>() {
			@Override
			public void onSuccess(Server is) {
				Session.iu.token = is.token;
				Cookies.setCookie("token", is.token);
			}
			@Override
			public void onFailure(Throwable caught) {
			}
		});
		
		
		// active subsribe over js
		Element eSubscribe = RootPanel.get("btn_subscribe").getElement();
		DOM.sinkEvents(eSubscribe, Event.ONCLICK);
		DOM.setEventListener(eSubscribe, new EventListener() {
			@Override
			public void onBrowserEvent(Event event) {
				Session.iu.email = email.getText();
				service.subscribe(Session.iu, new AsyncCallback<String>() {
					@Override
					public void onSuccess(String result) {
						infoLabel.setText(result);
					}
					
					@Override
					public void onFailure(Throwable caught) {
						infoLabel.setText(caught.toString());
					}
				});
			}
		});
		
		
		
		
		
		
		//Element e = RootPanel.get("discountPercent").getElement();
		//final Label x = new Label().wrap(RootPanel.get("discountPercent").getElement());
	
		
		
		
/*		HTMLPanel html = new HTMLPanel(rootPanel.getElement().getInnerHTML());
		html.addAndReplaceElement(btnSubscriber, "btn_subscribe");
*/		

	
}


	
	
/*	class SubscribeHandler implements ClickHandler {
		
		@Override
		public void onClick(ClickEvent event) {
			Window.confirm("Odeslano");
			
		}
	};
*/	

	
}
